﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Text;

namespace WebSite_CodeMan.AjaxCommon
{
    public partial class Ajax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadClass();
            }
        }
        private void LoadClass() 
        {
            string ClassType = Common.GetMes.GetRequestForm(Request, "ClassType");
            switch (ClassType)
            {
                case "NoteLeave":
                    NoteLeave();
                    break;
                case "SendLeave":
                    SendLeave();
                    break;
                case "GetLeave":
                    GetLeave();
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 异步获取评论的数量 并且同步更新浏览次数
        /// </summary>
        private void NoteLeave()
        {
            string strNID = Common.GetMes.GetRequestForm(Request,"NID");
            string strLeaveCount = "0";
            string strReadCount = "0";
            if (!string.IsNullOrEmpty(strNID))
            {
                strLeaveCount = BLL.LeaveInfo.GetLeaveCount(strNID);
                strReadCount = BLL.NoteInfo.GetReadCount(strNID);
                if (string.IsNullOrEmpty(Common.GetMes.GetCookies("Read_"+strNID)))
                {
                    BLL.NoteInfo.UpdateReadCount(strNID);
                    Common.SetMes.CreatCookies("Read_" + strNID, 1, strNID, "");
                }
            }
            Response.Write("{LeaveCount:" + strLeaveCount + ",ReadCount:" + strReadCount + "}");
        }
        /// <summary>
        /// 发表评论信息
        /// </summary>
        private void SendLeave()
        {
            string strNID = Common.GetMes.GetRequestForm(Request, "NID");
            string strNote = Common.GetMes.GetRequestForm(Request, "Note");
            string strName = Common.GetMes.GetRequestForm(Request, "tname");
            string strSex = Common.GetMes.GetRequestForm(Request, "sex");
            string strTel = Common.GetMes.GetRequestForm(Request, "tel");
            string strEmail = Common.GetMes.GetRequestForm(Request, "email");
            if (!string.IsNullOrEmpty(strNID))
            {
                Response.Write(BLL.LeaveInfo.AddLeaveMes(strNID, ((strNID == "-1") ? "1" : "0"), 
                    Server.HtmlEncode(Server.UrlDecode(strNote).Replace("\n"," ")),
                    Server.HtmlEncode(Server.UrlDecode(strName)),
                    Server.HtmlEncode(Server.UrlDecode(strSex)),
                    Server.HtmlEncode(Server.UrlDecode(strTel)),
                    Server.HtmlEncode(Server.UrlDecode(strEmail))
                    ));
            }
        }
        /// <summary>
        /// 获取一条信息的前几条评论信息
        /// </summary>
        private void GetLeave()
        {
            int i = 1;
            string strNID = Common.GetMes.GetRequestForm(Request, "NID");
            StringBuilder strJson = new StringBuilder();
            OleDbDataReader DataRead = BLL.LeaveInfo.GetAllLeaveByNoteInfo("5", strNID);
            if (DataRead.HasRows)
            {
                strJson.Append("{\"NID\":"+strNID+",\"AllNote\":{");
                while (DataRead.Read())
                {
                    strJson.Append("" + ((i == 1) ? "" : ",") + "\"T_LeaveNote" + i + "\":\"" + DataRead["T_LeaveNote"].ToString() + "\"");
                    strJson.Append(",\"T_LeaveReBack" + i + "\":\"" + DataRead["T_LeaveReBack"].ToString() + "\"");
                    strJson.Append(",\"T_LeaveIP" + i + "\":\"" + BaseClass.BaseClass.GetNameByIP(DataRead["T_LeaveIP"].ToString(),Page) + "\"");
                    strJson.Append(",\"T_DateTime" + i + "\":\"" + Convert.ToDateTime(DataRead["T_DateTime"].ToString()).ToString("yyyy年MM月dd日 HH点mm分") + "\"");
                    i++;
                }
                strJson.Append("},\"AllCount\":\""+i+"\"}");
            }
            DataRead.Close();
            DataRead.Dispose();
            Response.Write(strJson.ToString());
        }
    }
}
